#ifndef MYTHREAD_H
#define MYTHREAD_H
#include "BinInput.h"
#include "CornerGrid.h"
#include "Dream3DRender.h"
#include <QThread>

enum NodeType
{
  NODE_DEFAULT = 0,

  NODE_GRIDLAYER,
  NODE_GRIDFIELDTYPE,
  NODE_GRIDTIME,
  NODE_GRIDFIELD,

  NODE_WELLLAYER,
  NODE_WELLFIELDTYPE,
  NODE_WELLTIME,
  NODE_WELLFIELD,
};

struct SelectField
{
  NodeType dataType;
  int id = 0;  // 0 静态  1...动态
  QString time;
  QString field;
  // RenderNode node;
  class QStandardItem* item;
  SelectField()
  {
    dataType = NODE_DEFAULT;
    item     = nullptr;
  }
};
Q_DECLARE_METATYPE(SelectField)

class Worker : public QThread
{
  Q_OBJECT
public:
  explicit Worker(MessageStateCall function, QObject* parent = 0);
  ~Worker();

  void MessageFunction(QString msg, int state, float per);

  // 设置工作类型
  // 1 扫描网格数据
  // 2 加载网格数据
  // 3 加载井数据
  void SetWorkType(int type)
  {
    mType = type;
  }
  int WorkType()
  {
    return mType;
  }

  // 获取动态属性表
  std::vector<std::vector<Name_Loc>> GetDynamicTable();
  // 获取静态属性表
  std::vector<Name_Loc> GetStaticTable();
  // 设置网格数据路径和待加载的属性数组
  void SetGridPathAndField(QString path, QList<SelectField>& selFields)
  {
    mFilePath  = path;
    mSelFields = selFields;
  }
  // 设置井数据路径(绝对路径)和待加载的属性字段
  void SetWellPathAndField(const QString& path, const QList<QString>& wellField)
  {
    mWellPath  = path;
    mWellField = wellField;
  }
  // 获取偏移值
  vsg::dvec3 GetOffset()
  {
    return mGrid.GetOffset();
  }
  // 获取数据偏移后的中心点
  vsg::dvec3 GetCenterOffset()
  {
    return mCenter;
  }
  // 获取当前选择的属性字段指定IJK的属性值
  double GetValue(int iselfield, vsg::ivec3 ijk);
  // 获取内部网格对象指针
  CornerGrid* GetGrid()
  {
    return &mGrid;
  }
  // 生成和是否显示对象数据
  RenderData* NewRenderData();
  void DeleteRenderData(RenderData* pData);
  // 删除内部记录已获取的属性信息
  void DeleteHasAtts();
protected:
  void Filter();
  void run();
  MessageStateCall mFunction;
  QString mFilePath;
  CornerGrid mGrid;
  int mType;
  VEC_PropArray mAtts;
  MAP_PropArray mHasAtts;
  vsg::dvec3 mCenter     = vsg::dvec3(0, 0, 0);
  bool mbInitSceneCenter = false;
  QList<SelectField> mSelFields;

  //
  QString mWellPath;
  QList<QString> mWellField;

signals:
  // 给主线程发消息
  void sendMsgToMain(QString msg, int state, float per);
  void SendDataToMain(RenderData* render, int datatype);
public slots:
  // 接收主线程的消息
  void recMegFromMain(QString);
};

class ReadAttWorker : public QThread
{
public:
  ReadAttWorker(CornerGrid* pGrid, int index, std::string& field, PropArray* outAtts) : mGrid(pGrid), mIndex(index), mField(field), mpOutAtts(outAtts)
  {
  }
  void run();

protected:
  CornerGrid* mGrid = nullptr;
  int mIndex        = -1;  // 0  静态属性   1 .... 动态属性
  std::string mField;
  PropArray* mpOutAtts = nullptr;
};

#endif  // MYTHREAD_H